package com.cqut.forum.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqut.forum.entity.Relation;
import com.cqut.forum.entity.User;
import com.cqut.forum.service.FriendRelationService;
import com.cqut.forum.service.UserService;

/**
 * 好友关系action
 * @author yangxun
 */
@Controller
public class FriendRelationAction {
	
	@Resource
	private UserService userService;
	@Resource
	private FriendRelationService friendRelationService;
	/**
	 * 添加好友时通过昵称查找用户信息
	 * @param userName
	 * @return
	 */
	@RequestMapping("/friend/search_user")
	@ResponseBody
	public Map<String, Object> searchUser(String userName){
		User user = userService.getByName(userName);
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("userPhoto", user.getUserPhoto());
		result.put("userName", user.getUserName());
		result.put("userSex", user.getUserSex());
		
		return result;
	}
	
	/**
	 * 申请好友
	 * @param request
	 * @param friendName
	 * @param level
	 * @return
	 */
	@RequestMapping("/friend/add_friend")
	@ResponseBody
	public String applyFriend(HttpServletRequest request,String friendName,String level){
		if(!"申请".equals(level)&&!"黑名单".equals(level)&&!"关注".equals(level)){
			return "false";
		}
		User user = (User)request.getSession().getAttribute("user");
		friendRelationService.addFriendRelation(user, friendName, level);
		return "true";
	}
	
	/**
	 * 获取用户好友
	 * @param request
	 * @return
	 */
	@RequestMapping("/friend/friend_list")
	@ResponseBody
	public List<Map<String, Object>> friendList(HttpServletRequest request,int index,int size){
		User user = (User)request.getSession().getAttribute("user");
		List<Map<String, Object>> friends = friendRelationService.getFriendsByUserId(user.getUserId(),index,size);
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		
		for(Map<String, Object> u :friends){
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("userId", u.get("user_id"));
			m.put("userName", u.get("user_name"));
			m.put("userSex", u.get("user_sex"));
			m.put("userPlace", u.get("user_place"));
			m.put("image", u.get("user_photo"));
			m.put("isCare", ((Boolean)u.get("is_care"))?"关心":"好友");
			m.put("relationId", u.get("relation_id"));
			m.put("name", u.get("user_name"));
			//m.put("userAge", u.get("user_age")==null?"":((Date)u.get("user_age")).toLocaleString());
			m.put("news", null==u.get("user_sign")?"":"");
			result.add(m);
		}
		
		return result;
	}
	
	/**
	 * 解除好友关系
	 * @param request
	 * @param relationId
	 * @param isBack为true代表加入黑名单，关系全部解除。且不能再添加为好友，为false代表只是删除好友关系
	 * @return
	 */
	@RequestMapping("/friend/delete_relation")
	@ResponseBody
	public String deleteRelation(HttpServletRequest request,int userId,boolean isBack){
		User user = (User)request.getSession().getAttribute("user");
		Relation relation = friendRelationService.getRelation(user.getUserId(), userId);
		friendRelationService.deleteRelation(relation.getRelationId(), isBack);
		return "操作成功!";
	}
	
	/**
	 * 更新申请记录
	 * @param request
	 * @param relationId
	 * @param state
	 * @return
	 */
	@RequestMapping("/friend/update_apply")
	@ResponseBody
	public String updateApply(HttpServletRequest request,int relationId,boolean state){
		User user = (User)request.getSession().getAttribute("user");
		friendRelationService.updateApply(user,relationId,state);
		return "添加成功";
	}
	
	@RequestMapping("/friend/add_care")
	@ResponseBody
	public String addCare(HttpServletRequest request,int friendId){
		User user = (User)request.getSession().getAttribute("user");
		friendRelationService.addCare(user.getUserId(), friendId);
		return "true";
	}
	
	/**
	 * 查询申请添加好友的人员列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/friend/load_apply")
	@ResponseBody
	public List<Map<String, Object>> loadApplys(HttpServletRequest request){
		User user = (User)request.getSession().getAttribute("user");
		List<Map<String, Object>> applys = friendRelationService.loadApplys(user.getUserId());
		return applys;
	}
	
	/**
	 * 根据昵称查询好友
	 * @param userName
	 * @return
	 */
	@RequestMapping("/friend/user_list")
	@ResponseBody
	public List<Map<String, Object>> userList(String userName){
		List<Map<String, Object>> users = friendRelationService.loadUserByName(userName);
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		
		for(Map<String, Object> u :users){
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("userId", u.get("user_id"));
			m.put("userName", u.get("user_name"));
			m.put("userSex", u.get("user_sex"));
			m.put("userPlace", u.get("user_place"));
			m.put("userPhoto", u.get("user_photo"));
			result.add(m);
		}
		
		return result;
	}
	/**
	 * 统计好友数量
	 * @param request
	 * @return
	 */
	@RequestMapping("/friend/count_size")
	@ResponseBody
	public int countSize(HttpServletRequest request){
		User user = (User)request.getSession().getAttribute("user");
		int count = friendRelationService.countFriends(user.getUserId());
		return count;
	}
}
